//
//  MeModifyMobileViewController.m
//  DBAChain
//
//  Created by Mxionlly on 2018/9/25.
//  Copyright © 2018年 Evan. All rights reserved.
//

#import "MeModifyMobileViewController.h"
#import "taskUserManagerService.h"

@interface MeModifyMobileViewController ()
@property (weak, nonatomic) IBOutlet UIButton *codeButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *codeField;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutToHead;

@property (nonatomic, strong) NSTimer           *timer;
@property (nonatomic, assign) NSInteger         number;
@end

@implementation MeModifyMobileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"手机号修改";
    [self initUI];
}

-(void)initUI
{
    self.number = 60;
    [self initCodeView:@"发送验证码"];
    self.layoutToHead.constant = 110;
}

- (IBAction)getMobileAction:(id)sender {
    if (![Check isMobileNumber:self.phoneField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    if (![Check checkLenght:self.codeField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入验证码"];
        return;
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setField:self.phoneField.text value:@"newphone"];
    [dict setField:self.codeField.text value:@"verifyCode"];
    [DKProgressHUD showLoading];
    [taskUserManagerService getUpdatePhone:dict success:^(BaseModel *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            [DKProgressHUD showSuccessWithStatus:@"修改成功"];
            [[UserCenter shareCenter] refreshProfiles:^(dataGetUserInfo *profile) {
                
            }];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [DKProgressHUD showErrorWithStatus:response.message];
            return;
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
        return;
    }];
}


- (IBAction)getCodeAction:(id)sender {
    if (![Check isMobileNumber:self.phoneField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }else{
        [DKProgressHUD showLoadingWithStatus:@"发送中"];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setField:self.phoneField.text value:@"phone"];
        [dict setField:[UserCenter shareCenter].user.pid value:@"uid"];
        [taskUserManagerService getVerifyCode:dict success:^(dataGetVerifyCode *response, BOOL success) {
            [DKProgressHUD dismiss];
            if (success) {
                self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updataAction) userInfo:nil repeats:YES];
                self.codeButton.userInteractionEnabled = NO;
                return ;
            }
            [DKProgressHUD showErrorWithStatus:@"发送失败"];
        } failture:^(NSError *error, NSString *strDescription) {
            NSString *strErr = [error.userInfo objectForKey:@"NSLocalizedDescription"];
            [DKProgressHUD showErrorWithStatus:strErr];
        }];
    }
}

- (void)updataAction {
    self.number--;
    [self initCodeView:[NSString stringWithFormat:@"%lds",(long)self.number]];
    if (self.number <= 0) {
        [self closeTimer];
    }
}

- (void)closeTimer {
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
        self.number = 60;
        self.codeButton.userInteractionEnabled = YES;
        [self initCodeView:@"发送验证码"];
    }
}


-(void)initCodeView:(NSString *)title
{
    [self.codeButton setTitle:title forState:UIControlStateNormal];
    [self.codeButton setTitleColor:COLOR_main forState:UIControlStateNormal];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
